package com.elitel.frame.base.service.impl;

import com.elitel.common.utils.UUIDUtil;
import com.elitel.frame.base.service.AcAppStationService;
import com.elitel.frame.main.dao.AcAppStationMapper;
import com.elitel.frame.main.dao.ext.ExtAcAppStationMapper;
import com.elitel.frame.main.entity.acappcatalog.AcAppCatalogDTO;
import com.elitel.frame.main.entity.acappstation.AcAppStation;
import com.elitel.frame.main.entity.acappstation.AcAppStationExample;
import com.elitel.frame.main.entity.constant.TmpUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 访问控制-应用和测站关系(AcAppStation)表服务实现类
 *
 * @author makejava
 * @since 2023-11-11 15:15:44
 */
@Service("acAppStationService")
public class AcAppStationServiceImpl implements AcAppStationService {
    @Resource
    private ExtAcAppStationMapper acAppStationDao;

    @Resource
    private AcAppStationMapper acAppStationMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param stationId 主键
     * @return 实例对象
     */
    @Override
    public AcAppStation queryById(String stationId) {
        return this.acAppStationDao.queryById(stationId);
    }


    /**
     * 新增数据
     *
     * @param acAppStation 实例对象
     * @return 实例对象
     */
    @Override
    public AcAppStation insert(AcAppStation acAppStation) {
        this.acAppStationDao.insert(acAppStation);
        return acAppStation;
    }

    /**
     * 修改数据
     *
     * @param acAppStation 实例对象
     * @return 实例对象
     */
    @Override
    public AcAppStation update(AcAppStation acAppStation) {
        this.acAppStationDao.update(acAppStation);
        return this.queryById(acAppStation.getStationId());
    }

    /**
     * 通过主键删除数据
     *
     * @param stationId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String stationId) {
        return this.acAppStationDao.deleteById(stationId) > 0;
    }

    @Override
    public int save(AcAppCatalogDTO dto) {
        // 不用考虑编辑数据 就一个id        
        Date date = new Date();
        String catalogId = dto.getCatalogId();

        AcAppStationExample example = new AcAppStationExample();
        example.createCriteria().andCatalogIdEqualTo(catalogId);

        List<AcAppStation> appStationList = dto.getAppStationList();

        // 如果是空的 就先根据分组id将数据全部数据删除 然后直接返回
        if (CollectionUtils.isEmpty(appStationList)) {
            this.acAppStationMapper.deleteByExample(example);
            return 0;
        }

        // 根据分组查询数据 数据库中的数据
        List<AcAppStation> acAppStations = this.acAppStationMapper.selectByExample(example);
        // 数据库中数据的数量
        int dbNum = acAppStations.size();
        // 数据库中的数据
        List<String> dbIds = acAppStations.stream().map(AcAppStation::getStationId).collect(Collectors.toList());
        List<AcAppStation> insertList = new ArrayList<>();
        List<String> delIds = new ArrayList<>();
        // 用于存放参数中的id
        List<String> paramsIds = new ArrayList<>();

        // 查询数据 新增数据
        for (AcAppStation acAppStation : appStationList) {
            if (!dbIds.contains(acAppStation.getStationId())) {
                acAppStation.setCreatedTime(date);
                acAppStation.setCreatedUser(TmpUser.ADMIN);
                acAppStation.setStationId(UUIDUtil.randomUUID());
                acAppStation.setCatalogId(catalogId);
                insertList.add(acAppStation);
            }
            paramsIds.add(acAppStation.getStationId());
        }

        // 将要删除的数据筛选出来
        for (String dbId : dbIds) {
            // 参数中没有 数据库中有 就将此数据删除掉
            if (!paramsIds.contains(dbId)) {
                delIds.add(dbId);
            }
        }
        // 插入
        if (CollectionUtils.isNotEmpty(insertList)) {
            int iNum = this.acAppStationDao.insertBatch(insertList);
            // 数据数量增加
            dbNum += iNum;
        }
        // 删除
        if (CollectionUtils.isNotEmpty(delIds)) {
            example.clear();
            example.createCriteria().andStationIdIn(delIds);
            int delNum = this.acAppStationMapper.deleteByExample(example);
            // 数据数量减少
            dbNum -= delNum;
        }
        // 返回数据库中的数据数量
        return dbNum;
    }
}
